package cn.itcast.dao;

import cn.itcast.bean.UmPassword;
import cn.itcast.bean.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AddServlet {

    /**
     * 添加
     *
     * @param user
     */

    public int insert(User user) throws IOException {
//        try {
////            Class.forName("com.mysql.jdbc.Driver");
////
////            Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/db5","root","");
////
////            PreparedStatement preparedStatement = conn.prepareStatement("insert  into  t_user  values(null,?,?)");
////
////            preparedStatement.setString(1,user.getName());
////            preparedStatement.setInt(2,user.getAge());
////            preparedStatement.executeUpdate();
////        } catch (Exception e) {
////            e.printStackTrace();
////        }
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        int insert = sqlSession.insert("User.insert", user);
        is.close();
        sqlSession.close();
        return insert;

    }

    public List<User> selectAll() throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        List<User> users = sqlSession.selectList("User.selectAll");
        is.close();
        sqlSession.close();
        return users;
    }

    public User selectByid(Integer id) throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession();
        User user = sqlSession.selectOne("User.selectByid", id);
        resourceAsStream.close();
        sqlSession.close();
        return user;

    }

    public int delete(int id) throws IOException {
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        int update = sqlSession.update("User.delete", id);
        is.close();
        sqlSession.close();
        return update;
    }

    public void listUm(UmPassword i) throws IOException {
    /*    try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db5", "root", "");

            PreparedStatement preparedStatement = conn.prepareStatement("update   t_nump  set password=? where uid=?");
            preparedStatement.setString(1, i.getPassword());
            preparedStatement.setString(2, i.getUnum());

            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        sqlSession.update("UP.listUm",i);
        is.close();
        sqlSession.close();
    }
@Test
    public int regUm(UmPassword i) throws IOException {
      /*  try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db5", "root", "");

            PreparedStatement preparedStatement = conn.prepareStatement("insert  into  t_nump  values(null,?,?)");

            preparedStatement.setString(1, i.getUnum());
            preparedStatement.setString(2, i.getPassword());
            preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    System.out.println(i);
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        int insert = sqlSession.insert("UP.regUm",i);
        is.close();
        sqlSession.close();
        return insert;
    }

    public List<UmPassword> find() throws IOException {
       /* try {
            List<UmPassword>  users = new ArrayList<>();
            Class.forName("com.mysql.jdbc.Driver");

            Connection conn =  DriverManager.getConnection("jdbc:mysql://localhost:3306/db5","root","");

            PreparedStatement preparedStatement = conn.prepareStatement("select * from  t_nump");

            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                UmPassword user = new UmPassword();
                user.setId(resultSet.getInt("id"));
                user.setUnum(resultSet.getString("uid"));
                user.setPassword(resultSet.getString("password"));
                users.add(user);
            }

            return   users;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }*/
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        List<UmPassword> list = sqlSession.selectList("UP.find");
        is.close();
        sqlSession.close();
        return list;
    }
}

